package com.ruoyi.web.controller.base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BPaper;
import com.ruoyi.base.service.IBPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷Controller
 *
 * @author ruoyi
 * @date 2024-03-24
 */
@RestController
@RequestMapping("/base/paper")
public class BPaperController extends BaseController {
    @Autowired
    private IBPaperService bPaperService;

    /**
     * 查询试卷列表
     */
    @PreAuthorize("@ss.hasPermi('base:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(BPaper bPaper) {
        startPage();
        List<BPaper> list = bPaperService.selectBPaperList(bPaper);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表
     */
    @PreAuthorize("@ss.hasPermi('base:paper:export')")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BPaper bPaper) {
        List<BPaper> list = bPaperService.selectBPaperList(bPaper);
        ExcelUtil<BPaper> util = new ExcelUtil<BPaper>(BPaper.class);
        util.exportExcel(response, list, "试卷数据");
    }

    /**
     * 获取试卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:paper:query')")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId) {
        return success(bPaperService.selectBPaperByPaperId(paperId));
    }

    /**
     * 新增试卷
     */
    @PreAuthorize("@ss.hasPermi('base:paper:add')")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BPaper bPaper) {
        return toAjax(bPaperService.insertBPaper(bPaper));
    }

    /**
     * 修改试卷
     */
    @PreAuthorize("@ss.hasPermi('base:paper:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BPaper bPaper) {
        return toAjax(bPaperService.updateBPaper(bPaper));
    }

    /**
     * 删除试卷
     */
    @PreAuthorize("@ss.hasPermi('base:paper:remove')")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds) {
        return toAjax(bPaperService.deleteBPaperByPaperIds(paperIds));
    }

    /**
     * 生成试卷
     */
    @PreAuthorize("@ss.hasPermi('base:paper:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @GetMapping("/build/{paperIds}")
    public AjaxResult build(@PathVariable Long[] paperIds) {
        try {
            return toAjax(bPaperService.buildBPaperByPaperIds(paperIds));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
